package com.dd.ent.car.ui.camera;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.dd.ent.car.R;
import com.dd.ent.car.common.bean.video.Video;
import com.dd.ent.car.common.config.UIConfig;
import com.dd.ent.car.common.player.MediaPlayerAcceptor;
import com.dd.ent.car.common.util.ScreenUtil;
import com.dd.ent.car.service.user.UserService;
import com.dd.ent.car.service.video.VideoService;
import com.dd.ent.car.ui.BaseActivity;
import com.duanqu.qupai.permission.EasyPermissions;
import com.duanqu.qupai.sdk.android.QupaiManager;
import com.duanqu.qupai.sdk.android.QupaiService;
import com.pp.ent.base.log.MLog;
import com.pp.ent.core.manage.UIHandler;
import com.pp.ent.view.annotation.ContentView;
import com.pp.ent.view.annotation.Inject;
import com.pp.ent.view.annotation.ViewInject;

import java.util.List;


/**
 * Created by spring on 2017/3/9.
 */
@ContentView(R.layout.activity_camera)
public class CameraActivity extends BaseActivity implements EasyPermissions.PermissionCallbacks {
    private final static String TAG = CameraActivity.class.getSimpleName();

    private static final int RC_PERMISSION_PERM = 1;

    @ViewInject(R.id.fra_playerContainer)
    private FrameLayout mPlayerContainer;

    @ViewInject(R.id.et_title)
    private EditText edVideoTitle;
    @ViewInject(R.id.btn_complete)
    private TextView btnComplete;

    @Inject
    private VideoService videoService;
    @Inject
    private UserService userService;
    private String mStrNickname;
    private boolean mIsNicknameNotEmpty = false;
    private Intent mCurrentData;
    private String videoFile;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initData();
        permissionCheck();
        addListener();
    }

    private void addListener() {
        edVideoTitle.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                mStrNickname = s.toString().trim();
                mIsNicknameNotEmpty = mStrNickname.length() > 0;
                btnComplete.setEnabled(mIsNicknameNotEmpty);
            }
        });
        btnComplete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //videoService.addVideoToQiuNiu(videoFile);
            }
        });

    }

    private void initData() {
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ScreenUtil.getScreenWidth(this), ScreenUtil.getScreenWidth(this));
        mPlayerContainer.setLayoutParams(params);
    }

    private void startRecord() {
        QupaiService qupaiService = QupaiManager
                .getQupaiService(this);
        /**
         * 建议上面的initRecord只在application里面调用一次。这里为了能够开发者直观看到改变所以可以调用多次
         */
        qupaiService.showRecordPage(this, RequestCode.RECORDE_SHOW, true);

    }


    /**
     * 支持6.0动态权限
     */
    private void permissionCheck() {
        String[] perms = {Manifest.permission.CAMERA, Manifest.permission.RECORD_AUDIO, Manifest.permission.READ_EXTERNAL_STORAGE};
        if (EasyPermissions.hasPermissions(this, perms)) {
            // Have permissions, do the thing!
            startRecord();
        } else {
            // Ask for both permissions
            EasyPermissions.requestPermissions(this, getResources().getString(R.string.permissions_tips_camera_audio),
                    RC_PERMISSION_PERM, perms);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            mCurrentData = data;
            RecordResult result = new RecordResult(data);
            //得到视频地址，和缩略图地址的数组，返回十张缩略图
            String videoFile = result.getPath();
            String[] thum = result.getThumbnail();
            result.getDuration();

            MLog.debug("", "视频路径:" + videoFile + "图片路径:" + thum[0] + "Duration" + result.getDuration());
            Video video = new Video();
            video.setPlayUrl(videoFile);
            this.videoFile = videoFile;

        } else {
            if (resultCode == RESULT_CANCELED) {
                finish();
            }
        }
    }

    @Override
    public void onPermissionsGranted(int i, List<String> list) {
        if (i == RC_PERMISSION_PERM) {
            startRecord();
        }
    }

    @Override
    public void onPermissionsDenied(int i, List<String> list) {

    }

    @UIHandler(UIConfig.UPLOAD_VIDEO_QI_NIU_SUCCESS)
    public void updateVideoQiNiuResponse(String playUrl) {
        Video video = new Video();
        video.setPlayUrl(playUrl);
        video.setUser(userService.getUser());
        video.setTitle(mStrNickname);
        videoService.commitVideoInfo(video, userService.getUid());
    }

    @UIHandler(UIConfig.ADD_VIDEO_INFO_TO_SERVER_SUCCESS)
    public void addVideoInfoServerResponse() {
        Toast.makeText(this, "上传成功", Toast.LENGTH_LONG).show();
    }
}
